package top.yokey.shopnc.activity.main;

import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import top.yokey.base.base.MemberHttpClient;
import top.yokey.shopnc.R;
import top.yokey.shopnc.activity.base.LoginActivity;
import top.yokey.shopnc.activity.mine.AddressActivity;
import top.yokey.shopnc.activity.mine.DistributionActivity;
import top.yokey.shopnc.activity.mine.FootprintActivity;
import top.yokey.shopnc.activity.mine.InviteActivity;
import top.yokey.shopnc.activity.mine.PointsActivity;
import top.yokey.shopnc.activity.mine.PreDepositActivity;
import top.yokey.shopnc.activity.mine.PropertyActivity;
import top.yokey.shopnc.activity.mine.RechargeCardActivity;
import top.yokey.shopnc.activity.mine.RedPacketActivity;
import top.yokey.shopnc.activity.mine.SettingActivity;
import top.yokey.shopnc.activity.mine.SignActivity;
import top.yokey.shopnc.activity.mine.VoucherActivity;
import top.yokey.shopnc.activity.refund.RefundActivity;
import top.yokey.shopnc.activity.seller.SellerActivity;
import top.yokey.shopnc.base.BaseApplication;
import top.yokey.shopnc.base.BaseBusClient;
import top.yokey.shopnc.base.BaseConstant;
import top.yokey.shopnc.base.BaseCountTime;
import top.yokey.shopnc.base.BaseFragment;
import top.yokey.base.base.BaseHttpListener;
import top.yokey.shopnc.base.BaseImageLoader;
import top.yokey.shopnc.base.BaseShared;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.base.base.BaseToast;
import top.yokey.base.bean.BaseBean;
import top.yokey.base.bean.MemberAssetBean;
import top.yokey.base.bean.MemberBean;
import top.yokey.base.event.MessageCountEvent;
import top.yokey.base.model.MemberAccountModel;
import top.yokey.base.model.MemberChatModel;
import top.yokey.base.model.MemberIndexModel;
import top.yokey.base.util.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

/**
 * @author MapStory
 * @ qq 1002285057
 * @ Project https://gitee.com/MapStory/ShopNc-Android
 */

@ContentView(R.layout.fragment_main_mine)
public class MineFragment extends BaseFragment {

    @ViewInject(R.id.mainLinearLayout)
    private LinearLayoutCompat mainLinearLayout;
    @ViewInject(R.id.mineLinearLayout)
    private LinearLayoutCompat mineLinearLayout;
    @ViewInject(R.id.avatarImageView)
    private AppCompatImageView avatarImageView;
    @ViewInject(R.id.nicknameTextView)
    private AppCompatTextView nicknameTextView;

    @ViewInject(R.id.goodsTextView)
    private AppCompatTextView goodsTextView;
    @ViewInject(R.id.storeTextView)
    private AppCompatTextView storeTextView;
    @ViewInject(R.id.footprintTextView)
    private AppCompatTextView footprintTextView;
    @ViewInject(R.id.signTextView)
    private AppCompatTextView signTextView;

    @ViewInject(R.id.orderRelativeLayout)
    private RelativeLayout orderRelativeLayout;
    @ViewInject(R.id.waitPaymentRelativeLayout)
    private RelativeLayout waitPaymentRelativeLayout;
    @ViewInject(R.id.waitReceiptRelativeLayout)
    private RelativeLayout waitReceiptRelativeLayout;
    @ViewInject(R.id.waitTakesRelativeLayout)
    private RelativeLayout waitTakesRelativeLayout;
    @ViewInject(R.id.waitEvaluateRelativeLayout)
    private RelativeLayout waitEvaluateRelativeLayout;
    @ViewInject(R.id.waitReturnRelativeLayout)
    private RelativeLayout waitReturnRelativeLayout;
    @ViewInject(R.id.waitPaymentDotTextView)
    private AppCompatTextView waitPaymentDotTextView;
    @ViewInject(R.id.waitReceiptDotTextView)
    private AppCompatTextView waitReceiptDotTextView;
    @ViewInject(R.id.waitTakesDotTextView)
    private AppCompatTextView waitTakesDotTextView;
    @ViewInject(R.id.waitEvaluateDotTextView)
    private AppCompatTextView waitEvaluateDotTextView;
    @ViewInject(R.id.waitReturnDotTextView)
    private AppCompatTextView waitReturnDotTextView;

    @ViewInject(R.id.propertyRelativeLayout)
    private RelativeLayout propertyRelativeLayout;
    @ViewInject(R.id.preDepositTextView)
    private AppCompatTextView preDepositTextView;
    @ViewInject(R.id.rechargeCardTextView)
    private AppCompatTextView rechargeCardTextView;
    @ViewInject(R.id.voucherTextView)
    private AppCompatTextView voucherTextView;
    @ViewInject(R.id.redPacketTextView)
    private AppCompatTextView redPacketTextView;
    @ViewInject(R.id.pointsTextView)
    private AppCompatTextView pointsTextView;

    @ViewInject(R.id.inviteRelativeLayout)
    private RelativeLayout inviteRelativeLayout;
    @ViewInject(R.id.distributionRelativeLayout)
    private RelativeLayout distributionRelativeLayout;
    @ViewInject(R.id.addressRelativeLayout)
    private RelativeLayout addressRelativeLayout;
    @ViewInject(R.id.sellerRelativeLayout)
    private RelativeLayout sellerRelativeLayout;
    @ViewInject(R.id.settingRelativeLayout)
    private RelativeLayout settingRelativeLayout;

    @Override
    public void initData() {

        if (BaseApplication.get().isLogin()) {
            getData();
            //getMessageCount();
        }

    }

    @Override
    public void initEven() {

        mineLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startCheckLogin(getActivity(), SettingActivity.class);
            }
        });

        goodsTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startFavorites(getActivity(), 0);
            }
        });

        storeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startFavorites(getActivity(), 1);
            }
        });

        footprintTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startCheckLogin(getActivity(), FootprintActivity.class);
            }
        });

        signTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startCheckLogin(getActivity(), SignActivity.class);
            }
        });

        orderRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startOrder(getActivity(), 0);
            }
        });

        waitPaymentRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startOrder(getActivity(), 1);
            }
        });

        waitReceiptRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startOrder(getActivity(), 2);
            }
        });

        waitTakesRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startOrder(getActivity(), 3);
            }
        });

        waitEvaluateRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startOrder(getActivity(), 4);
            }
        });

        waitReturnRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startCheckLogin(getActivity(), RefundActivity.class);
            }
        });

        propertyRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!BaseApplication.get().isLogin()) {
                    BaseApplication.get().start(getActivity(), LoginActivity.class);
                    return;
                }
                if (BaseApplication.get().getMemberAssetBean() == null) {
                    BaseSnackBar.get().showHandler(mainLinearLayout);
                    return;
                }
                BaseApplication.get().start(getActivity(), PropertyActivity.class);
            }
        });

        preDepositTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!BaseApplication.get().isLogin()) {
                    BaseApplication.get().start(getActivity(), LoginActivity.class);
                    return;
                }
                if (BaseApplication.get().getMemberAssetBean() == null) {
                    BaseSnackBar.get().showHandler(mainLinearLayout);
                    return;
                }
                BaseApplication.get().start(getActivity(), PreDepositActivity.class);
            }
        });

        rechargeCardTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!BaseApplication.get().isLogin()) {
                    BaseApplication.get().start(getActivity(), LoginActivity.class);
                    return;
                }
                if (BaseApplication.get().getMemberAssetBean() == null) {
                    BaseSnackBar.get().showHandler(mainLinearLayout);
                    return;
                }
                BaseApplication.get().start(getActivity(), RechargeCardActivity.class);
            }
        });

        voucherTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!BaseApplication.get().isLogin()) {
                    BaseApplication.get().start(getActivity(), LoginActivity.class);
                    return;
                }
                if (BaseApplication.get().getMemberAssetBean() == null) {
                    BaseSnackBar.get().showHandler(mainLinearLayout);
                    return;
                }
                BaseApplication.get().start(getActivity(), VoucherActivity.class);
            }
        });

        redPacketTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!BaseApplication.get().isLogin()) {
                    BaseApplication.get().start(getActivity(), LoginActivity.class);
                    return;
                }
                if (BaseApplication.get().getMemberAssetBean() == null) {
                    BaseSnackBar.get().showHandler(mainLinearLayout);
                    return;
                }
                BaseApplication.get().start(getActivity(), RedPacketActivity.class);
            }
        });

        pointsTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!BaseApplication.get().isLogin()) {
                    BaseApplication.get().start(getActivity(), LoginActivity.class);
                    return;
                }
                if (BaseApplication.get().getMemberAssetBean() == null) {
                    BaseSnackBar.get().showHandler(mainLinearLayout);
                    return;
                }
                BaseApplication.get().start(getActivity(), PointsActivity.class);
            }
        });

        inviteRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startCheckLogin(getActivity(), InviteActivity.class);
            }
        });

        distributionRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startCheckLogin(getActivity(), DistributionActivity.class);
            }
        });

        addressRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startCheckLogin(getActivity(), AddressActivity.class);
            }
        });

        sellerRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApplication.get().startCheckSellerLogin(getActivity(), SellerActivity.class);
            }
        });

        settingRelativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseApplication.get().startCheckLogin(getActivity(), SettingActivity.class);
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        if (BaseApplication.get().isLogin()) {
            getData();
        } else {
            nicknameTextView.setText("请登录");
        }
    }

    //自定义方法

    private void getData() {

        MemberIndexModel.get().index(new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                String data = JsonUtil.getDatasString(baseBean.getDatas(), "member_info");
                BaseApplication.get().setMemberBean(JsonUtil.json2Bean(data, MemberBean.class));
                BaseImageLoader.get().displayCircle(BaseApplication.get().getMemberBean().getAvatar(), avatarImageView);
                nicknameTextView.setText(BaseApplication.get().getMemberBean().getUserName());
                goodsTextView.setText("商品");
                if (!TextUtils.isEmpty(BaseApplication.get().getMemberBean().getFavoritesGoods())) {
                    goodsTextView.append("：" + BaseApplication.get().getMemberBean().getFavoritesGoods());
                }
                storeTextView.setText("店铺");
                if (!TextUtils.isEmpty(BaseApplication.get().getMemberBean().getFavoritesStore())) {
                    storeTextView.append("：" + BaseApplication.get().getMemberBean().getFavoritesStore());
                }
                waitPaymentDotTextView.setVisibility(BaseApplication.get().getMemberBean().getOrderNopayCount().equals("0") ? View.GONE : View.VISIBLE);
                waitReceiptDotTextView.setVisibility(BaseApplication.get().getMemberBean().getOrderNoreceiptCount().equals("0") ? View.GONE : View.VISIBLE);
                waitTakesDotTextView.setVisibility(BaseApplication.get().getMemberBean().getOrderNotakesCount().equals("0") ? View.GONE : View.VISIBLE);
                waitEvaluateDotTextView.setVisibility(BaseApplication.get().getMemberBean().getOrderNoevalCount().equals("0") ? View.GONE : View.VISIBLE);
                waitReturnDotTextView.setVisibility(BaseApplication.get().getMemberBean().getReturnX().equals("0") ? View.GONE : View.VISIBLE);
                getMobileInfo();
            }

            @Override
            public void onFailure(String reason) {
                if (reason.equals("请登录")) {
                    MemberHttpClient.get().updateKey("");
                    BaseToast.get().show(reason);
                    BaseShared.get().putString(BaseConstant.SHARED_KEY, "");
                    BaseApplication.get().start(getActivity(), LoginActivity.class);
                } else {
                    new BaseCountTime(BaseConstant.TIME_COUNT, BaseConstant.TIME_TICK) {
                        @Override
                        public void onFinish() {
                            super.onFinish();
                            getData();
                        }
                    }.start();
                }
            }
        });

    }

    private void getMobileInfo() {

        MemberAccountModel.get().getMobileInfo(new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                try {
                    JSONObject jsonObject = new JSONObject(baseBean.getDatas());
                    BaseApplication.get().getMemberBean().setMobielState(jsonObject.getBoolean("state"));
                    BaseApplication.get().getMemberBean().setUserMobile(jsonObject.getString("mobile"));
                    getMemberAsset();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String reason) {
                new BaseCountTime(BaseConstant.TIME_COUNT, BaseConstant.TIME_TICK) {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        getMobileInfo();
                    }
                }.start();
            }
        });

    }

    private void getMemberAsset() {

        MemberIndexModel.get().myAsset(new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                BaseApplication.get().setMemberAssetBean(JsonUtil.json2Bean(baseBean.getDatas(), MemberAssetBean.class));
            }

            @Override
            public void onFailure(String reason) {
                new BaseCountTime(BaseConstant.TIME_MESSAGE, BaseConstant.TIME_TICK) {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        getMemberAsset();
                    }
                }.start();
            }
        });

    }

    private void getMessageCount() {

        MemberChatModel.get().getMsgCount(new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                if (!baseBean.getDatas().equals("0")) {
                    BaseBusClient.get().post(new MessageCountEvent(true));
                }
                new BaseCountTime(BaseConstant.TIME_COUNT, BaseConstant.TIME_TICK) {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        getMessageCount();
                    }
                }.start();
            }

            @Override
            public void onFailure(String reason) {
                new BaseCountTime(BaseConstant.TIME_COUNT, BaseConstant.TIME_TICK) {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        getMessageCount();
                    }
                }.start();
            }
        });

    }

}
